<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use catetree\Catetree;
class Article extends Controller
{
	public function lst()
	{	
		$articleRes=db('article')->alias('a')->field('a.*,c.cate_name')->join('cate c','a.cat_id=c.id')->paginate('10');
		$settitle=settitle('文章');
		$this->assign('settitle',$settitle);
		$this->assign('articleRes',$articleRes);
		return view();
	}
	public function add()
	{		
		$cate=new Catetree();
		$cateObj=db('cate');
		if(request()->isPost())
		{
			$data=input('post.');
			$data['addtime']=time();
			//链接的处理
			if($data['link_url']&&stripos($data['link_url'],'http://')===false)
			{
				$data['link_url']='http://'.$data['link_url'];
			}
			//验证
    		$validate = validate('Article');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			//处理上传图片
			if($_FILES['thumb']['tmp_name'])
			{
				$imgname='thumb';
				$filename=IMG_UPLOADS.DS.'article';
				$data['thumb']=uploadsingle($imgname,$filename);
			}
			$add=db('article')->insert($data);
			if($add)
			{
				$this->success('添加文章成功',url('lst'));
			}else
			{
				$this->error('添加文章失败');
			}
		}
		$settitle=settitle('文章');
		$cateRes=$cateObj->order('sort desc')->select();
		$cateRes=$cate->catetree($cateRes);
		$this->assign(
			array(
				'settitle'=>$settitle,
				'cateRes'=>$cateRes
			)
		);
		return view();
	}

	public function edit()
	{	
		$id=input('id');
		$cate=new Catetree();
		$cateObj=db('cate');
		if(request()->isPost())
		{
			$data=input('post.');
			//链接的处理
			if($data['link_url']&&stripos($data['link_url'],'http://')===false)
			{
				$data['link_url']='http://'.$data['link_url'];
			}
			//验证
    		$validate = validate('Article');
    		if(!$validate->check($data)){
			    $this->error($validate->getError());
			}
			//处理上传图片
			if($_FILES['thumb']['tmp_name'])
			{
				$this->delbrandimg($id);
				$imgname='thumb';
				$filename=IMG_UPLOADS.DS.'article';
				$data['thumb']=uploadsingle($imgname,$filename);
			}
			$save=db('article')->update($data);
			if($save!=false)
			{
				$this->success('修改文章成功',url('lst'));
			}else
			{
				$this->error('修改文章失败');
			}
		}
		$settitle=settitle('文章');
		$articles=db('article')->find($id);
		$cateRes=$cateObj->order('sort desc')->select();
		$cateRes=$cate->catetree($cateRes);
		$this->assign(
			array(
				'settitle'=>$settitle,
				'cateRes'=>$cateRes,
				'articles'=>$articles
			)
		);
		return view();
	}

	public function del($id)
	{	
		$this->delbrandimg($id);
		$del=db('article')->delete($id);
		if($del)
		{
			$this->success('删除文章成功',url('lst'));
		}else
		{
			$this->error('删除文章失败');
		}
	}

	//文章图片的删除
	public function delbrandimg($id)
	{
		//查询是否有文章图片 如果有则删除
		$imgurl=db('article')->field('thumb')->find($id);
		$imgurl=$imgurl['thumb'];
		if(!empty($imgurl))
		{
			$fileurl=IMG_UPLOADS.DS.'article';
			$filename=$fileurl.'\\'.$imgurl;
			if(file_exists($filename))
			{
				@unlink($filename);
			}
		}
	}

	public function imglist()
	{
		$file_dir=myscandir();
		$files=array();
        foreach ($file_dir as $k => $v) {
          if(is_array($v)){
            foreach ($v as $k1 => $v1) {
                $v1=str_replace(UEDITOR, HTTP_UEDITOR, $v1);
                $files[]=$v1;
            }
          }else{
            $v=str_replace(UEDITOR, HTTP_UEDITOR, $v);
            $files[]=$v;
          }
        }
		$settitle=settitle('图片');
		$this->assign(array(
			'settitle'=>$settitle,
			'imgRes'=>$files
		));
		return view();
	}

	//ajax 异步删除图片
    public function delimg(){
        $imgsrc=input('imgsrc');
        $imgsrc=DEL_UEDITOR.$imgsrc;
        if(file_exists($imgsrc)){
            if(@unlink($imgsrc)){
                echo 1;
            }else{
                echo 2;
            }
        }else{
            echo 3;
        }
    }

}
